package com.dt76.smallloanbackendservice.service.etao.impl;

import com.dt76.smallloanbackendservice.mapper.etao.LoginLogMapper;
import com.dt76.smallloanbackendservice.pojo.etao.LoginLog;
import com.dt76.smallloanbackendservice.service.etao.LoginLogService;
import com.dt76.smallloanbackendservice.utils.DateUtils;
import com.dt76.smallloanbackendservice.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;

    @Override
    public boolean addLoginMsg(LoginLog loginLog) {
        boolean fg = false;
        if (loginLogMapper.addLoginMsg(loginLog) == 1) {
            fg = true;
        }
        return fg;
    }

    @Override
    public Page getLoginLog(Integer pageNum, String empName, String startTime, String endTime) {
        Page page = new Page();
        page.setPageIndex(pageNum);
        page.setTotalCount(loginLogMapper.getTotalCount(empName, startTime, endTime));
        List<LoginLog> loginLogs = loginLogMapper.getLoginLog(empName,startTime,endTime,
                                                                (page.getPageIndex()-1)*page.getPageSize(),
                                                                page.getPageSize());
        for (LoginLog loginLog : loginLogs) {
            loginLog.setLogTimeStr(DateUtils.dateToString(loginLog.getLogTime(),"yyyy-MM-dd HH:mm:ss"));
        }
        page.setRows(loginLogs);
        return page;
    }

}
